<!DOCTYPE HTML>
<html>
<head>
    <title>Error</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <script src="/scripts/miniui/miniui.js"></script>
    <!--<script src="/scripts/move.js"></script>-->
    <link rel="stylesheet" href="/stylesheets/formdata.css">
    <script src="/qiniuJs/goodsFormdata.js"></script>
    <script>
      var domain = "<%= domain %>"; // you bucket domain  eg: http://xxx.bkt.clouddn.com
    </script>
</head>
<body>
<div class="container" style="text-align:center;">
    <!-- Document：https://developer.qiniu.com/kodo/manual/form-upload -->

    <div style="text-align: left;margin-left: 40px;">
        <span style="width:80px;display:inline-block;">商品昵称：</span>
        <input id="title" class="mini-textbox" style="width:155px;" value='<%= result.data[0].title%>' />

        <span style="width:80px;display:inline-block;margin-left: 30px">库存：</span>
        <input id="stock" class="mini-textbox" vtype="float" style="width:155px;" value='<%= result.data[0].stock%>' />

        <br /><br />

        <span style="width:80px;display:inline-block;">商品状态：</span>
        <input id="status" class="mini-combobox" style="width:155px;" data="shopStatus" value='<%= result.data[0].status%>'/>

        <span style="width:80px;display:inline-block;margin-left: 30px">能否团购：</span>
        <input id="groupBuying" class="mini-combobox" field="id" style="width:155px;" data="groupBuying" value='<%= result.data[0].groupBuying%>'/>

        <br /><br />

        <span style="width:80px;display:inline-block;">是否包邮：</span>
        <input id="freePost"  class="mini-combobox" style="width:155px;" data="freePost" value='<%= result.data[0].freePost%>'/>


        <span style="width:80px;display:inline-block;margin-left: 30px">团购价格：</span>
        <input id="groupPrice" class="mini-textbox"  style="width:155px;" vtype="float" required="true" value="<%= result.data[0].groupPrice%>"/>

        <br /><br />

        <span style="width:80px;display:inline-block;">销量：</span>
        <input id="saleNums" class="mini-textbox"  style="width:155px;"  vtype="int" value="<%= result.data[0].saleNums%>"/>

        <span style="width:80px;display:inline-block;margin-left: 30px">原价：</span>
        <input id="originalPrice" class="mini-textbox"  onvaluechanged="setPrice()" style="width:155px;"  vtype="float" required="true" value="<%= result.data[0].originalPrice%>"/>

        <br /><br />
        <span style="width:80px;display:inline-block;">价格：</span>
        <input id="price" class="mini-textbox" onvaluechanged="setDiscount()" style="width:155px;"  vtype="float"  required="true" value="<%= result.data[0].price%>"/>

        <span style="width:80px;display:inline-block;margin-left: 30px">能否订制：</span>
        <input id="customize" class="mini-combobox"   style="width:155px;" data="customize"  value="<%= result.data[0].customize%>"/>

        <br /><br />
        <span style="width:80px;display:inline-block;">商品折扣：</span>
        <input id="discount" class="mini-textbox"  onvaluechanged="setPrice()"  style="width:155px;" vtype="range:0,10"  required="true"  value="<%= result.data[0].discount%>"/>

        <span style="width:80px;display:inline-block;margin-left: 30px">推广佣金：</span>
        <input id="promotePrice" class="mini-textbox" vtype="float" onvaluechanged="setPromotionPrice()" style="width:155px;" value='<%= result.data[0].promotePrice%>' />
        <br /><br />
        <span style="width:80px;display:inline-block;">商品类型：</span>
        <input id="goodsType" class="mini-combobox" valueField="objectId" style="width:155px;" data="goodsTypesList" required="true"  value="<%= result.data[0].goodsType%>" />
        <input id="downUrl" class="mini-textbox" style="display: none"/>
        <br /><br />
        <span style="width:80px;display:inline-block;">商品描述：</span>
        <textarea id="describe" class="mini-textarea"  value="<%= result.data[0].describe%>" style="width: 430px;height: 100px"></textarea>
    </div>

    <br /><br />
    <div style="margin-left: 20px">
        <div style="width:535px;">
            <div class="mini-toolbar" style="border-bottom:0;padding:0px;width:533; text-align: left">
                <table style="width:100%;">
                    <tr>
                        <td style="width:100%">
                            <a class="mini-button" iconCls="icon-add" onclick="addRow()" plain="true" tooltip="增加...">增加</a>
                            <a class="mini-button" iconCls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                            <span class="separator"></span>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div id="datagrid1" class="mini-datagrid" style="width:535px;height:280px;"
             url="/getdata/getGoodsEditModelArray" idField="id" allowResize="true"
             sizeList="[20,30,50,100]" pageSize="30"
             allowCellEdit="true" allowCellSelect="true" multiSelect="true"
             editNextOnEnterKey="true" editNextRowCell="true"
        >
            <div property="columns">
                <div type="indexcolumn"></div>
                <div type="checkcolumn"></div>
                <div name="model" field="model" headerAlign="center"  width="150">商品型号
                    <input property="editor" class="mini-textbox" style="width:100%;" minWidth="100"/>
                </div>
                <div name="stock" field="stock" width="100" >商品库存
                    <input property="editor" class="mini-spinner" minValue="0" maxValue="999999999" style="width:100%;"/>
                </div>
                <!--ComboBox：本地数据-->

                <!--ComboBox：远程数据-->
            </div>
        </div>
    </div>
    <br /><br />
    <div style="margin: 0 auto;text-align: center">
        <a class="mini-button " iconCls="icon-ok" onclick="updataUser()">确定</a>
        <a class="mini-button" iconCls="icon-cancel" onclick="onCancel">取消</a>
    </div>
    <br /><br />
</div>
</body>
<script>

    var goodsTypesList = JSON.parse('<%- goodsTypesList %>');
    var shopStatus = [{ id:0 , text:'下架' }, { id: -1, text:'删除'},{id:1,text:'上架'},{id:2,text:'默认'}];
    var groupBuying = [{ id:1,text:'可以团购'},{id:0,text:'不能团购'}];
    var freePost = [{id:1,text:'包邮'},{id:0,text:'不包邮'}];
    var customize = [{id:1,text:'可以定制'},{id:-1,text:'不可以定制'}];
    var objectId = '<%= result.data[0].objectId%>';
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load({
      objectId:'<%- objectId %>'
    });
    function updataUser() {
      grid.selectAll()
        var data = grid.getSelecteds();
        var model = mini.encode(data);
        console.log(model)
        var title = mini.get("title").getValue();
        var promotePrice = mini.get("promotePrice").getValue();
        var status = mini.get("status").getValue();
        var groupBuying = Number(mini.get("groupBuying").getValue());
        var freePost = mini.get("freePost").getValue();
        var groupPrice = mini.get("groupPrice").getValue();
        var saleNums = mini.get("saleNums").getValue();
        var originalPrice = mini.get("originalPrice").getValue();
        var price = mini.get("price").getValue();
        var customize = mini.get("customize").getValue();
        var discount = mini.get("discount").getValue();
        var describe = mini.get("describe").getValue();
        var stock = mini.get("stock").getValue();
        var goodsTypeOId = mini.get("goodsType").getValue();
        for (let item of goodsTypesList){
          if (goodsTypeOId == item.text) {
            goodsTypeOId = item.objectId
          }
        }
        $.ajax({
            type: "POST",
            url: "/getdata/editGoods",
            data: {
                objectId: objectId,
                title: title,
                promotePrice: promotePrice,
                status: status,
                groupBuying: groupBuying,
                freePost: freePost,
                groupPrice: groupPrice,
                saleNums: saleNums,
                originalPrice: originalPrice,
                price: price,
                customize: customize,
                discount: discount,
                describe: describe,
                stock:stock,
                goodsTypeOId:goodsTypeOId,
                model: model
            },
            success: function (result) {
                if (result=='1') {
                    CloseWindow("save");
                }else{
                    alert("请填写正确数据");
                }
            }
        })

    }
    //    关闭窗口
    function CloseWindow(action) {
        if (action == "close" && form.isChanged()) {
            if (confirm("数据被修改了，是否先保存？")) {
                return false;
            }
        }
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();
    }
    function onOk(e) {
        SaveData();
    }
    function onCancel(e) {
        CloseWindow("cancel");
    }

//    function priceChange() {
//        console.log("aaaa");
//        setOriginalPrice();
//    }
    function setPrice() {
        var price = mini.get("price");
        var discount = mini.get("discount").getValue();
        var originalPrice = mini.get("originalPrice").getValue();
        var priceValue = (originalPrice*discount/10);
        price.setValue(priceValue);
    }
    function setDiscount() {
        var discount = mini.get("discount");
        var price = mini.get("price").getValue();
        var priceValue = mini.get("price");
        var originalPrice = mini.get("originalPrice").getValue();
        if(originalPrice!=''){
            if (price<=originalPrice){
                var discountValue = (price/originalPrice*10).toFixed(1);
                discount.setValue(discountValue);
            }else {
                alert("价格不能大于原价");
                priceValue.setValue(originalPrice);
                discount.setValue(10);
            }
        }

    }
    function setPromotionPrice() {
        var price = mini.get("price").getValue();
        var promotePrice = mini.get("promotePrice").getValue();
        var promotionPriceValue = mini.get("promotePrice");
        if (promotePrice>price){
            alert("推广佣金最大不能超过价格");
            promotionPriceValue.setValue(price);
        }
    }
    function addRow() {
      var newRow = { name: "New Row" };
      grid.addRow(newRow, 0);

      grid.beginEditCell(newRow, "LoginName");
    }
    function removeRow() {
      var rows = grid.getSelecteds();
      if (rows.length > 0) {

        grid.removeRows(rows, true);
      }
    }
    function bigImg(e) {
      console.log(e)
      // var row = grid.getSelected();
      // if (row) {
      //   if(flag==1) {
      //     var path = row.shopIcon;
      //     $("#imgId").attr('src', path);
      //     $("#outImg").css('display','block');
      //   }
      // }
    }
    function smallImg() {
      $("#outImg").css('display','none');
    }
</script>
</html>